/**
 * 描述：这是一个利用链表作为基础数据结构来完成的一个栈结构
 *
 * @author WangB
 * @create 2018-10-19-23:59
 */
public class LinkedListStack<E> implements Stack<E>{

    private LinkedList<E> list;//私有的链表对象
    public LinkedListStack(){
        list = new LinkedList <> ();
    }

    @Override
    public int getSize(){
        return list.getSize ();
    }

    @Override
    public boolean isEmpty(){
        return list.isEmpty ();
    }

    @Override
    public void push(E e){
        list.addFirst ( e );
    }

    @Override
    public E peek(){
        return list.getFirst ();
    }

    @Override

    public String toString(){
        StringBuilder ret = new StringBuilder (  );
        ret.append ( "Stack: top " );
        ret.append ( list );
        return ret.toString ();
    }


    @Override
    public E pop(){
        return list.removeFirst ();
    }


    public static void main(String[] args) {
        LinkedListStack<Integer> stack = new LinkedListStack<>();

        for (int i = 0; i < 5 ; i++) {
            stack.push(i);
            System.out.println(stack);
        }

        stack.pop();
        System.out.println(stack);
    }
}
